//
//  CargoBoxTableViewCell.m
//  OrderAssistant
//
//  Created by chendong on 2020/8/26.
//  Copyright © 2020 chendong. All rights reserved.
//

#import "CargoBoxTableViewCell.h"
@interface CargoBoxTableViewCell()
@property(nonatomic,strong)UILabel *ddbhL;
@property(nonatomic,strong)UILabel *xdrqL;
@property(nonatomic,strong)UILabel *xdbmL;
@property(nonatomic,strong)UILabel *zdrL;
@property(nonatomic,strong)UILabel *zdrLP;
@end
@implementation CargoBoxTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *bgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 100)];
        bgV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:bgV];
        
        UILabel *ddbhL = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, bgV.frame.size.width-10, 35)];
        ddbhL.text = @"大米";
        ddbhL.textColor = [UIColor blackColor];
        ddbhL.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
        [bgV addSubview:ddbhL];
        self.ddbhL = ddbhL;
        
        UILabel *xdrqL = [[UILabel alloc]initWithFrame:CGRectMake(ddbhL.frame.origin.x, CGRectGetMaxY(ddbhL.frame), 0, 35)];
        xdrqL.text = @"00312321";
        xdrqL.textColor = [UIColor blackColor];
        [bgV addSubview:xdrqL];
        self.xdrqL = xdrqL;
        
        UILabel *zdrLP = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(xdrqL.frame), CGRectGetMaxY(ddbhL.frame), ddbhL.frame.size.width/3, 30)];
        zdrLP.text = @"30元";
        zdrLP.textColor = [UIColor blackColor];
        [bgV addSubview:zdrLP];
        self.zdrLP = zdrLP;
        
        UILabel *zdrL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(xdrqL.frame), CGRectGetMaxY(zdrLP.frame), ddbhL.frame.size.width/3, 35)];
        zdrL.text = @"1*50";
        zdrL.textColor = [UIColor blackColor];
        [bgV addSubview:zdrL];
        self.zdrL = zdrL;
        
        UILabel *xdbmLu = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(zdrL.frame), CGRectGetMaxY(ddbhL.frame), ddbhL.frame.size.width-CGRectGetMaxX(self.zdrLP.frame), 30)];
        xdbmLu.text = @"¥30";
        xdbmLu.textColor = [UIColor blackColor];
        xdbmLu.textAlignment = NSTextAlignmentLeft;
        [bgV addSubview:xdbmLu];
        self.xdbmLu = xdbmLu;
        
        UILabel *xdbmL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(zdrL.frame), CGRectGetMaxY(zdrLP.frame), ddbhL.frame.size.width/3-40, 35)];
        xdbmL.text = @"袋";
        xdbmL.textColor = [UIColor blackColor];
        xdbmL.textAlignment = NSTextAlignmentLeft;
        [bgV addSubview:xdbmL];
        self.xdbmL = xdbmL;
        
        UIView *numBgV = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(xdbmL.frame), CGRectGetMaxY(zdrLP.frame), bgV.frame.size.width-CGRectGetMaxX(xdbmL.frame), 35)];
        [bgV addSubview:numBgV];
        NSArray *arr = @[@"-",@"",@"+"];
        NSArray *imgArr = @[@"",@"",@"五一路凉菜库_05.png"];
//        NSArray *imgArrr = @[@"五一路凉菜库_03.png",@"",@"五一路凉菜库_05.png"];
        NSArray *imgArrr = @[@"linshi1.png",@"",@"linshi2.png"];
        int x;
        int y=0;
        int w=numBgV.frame.size.width/arr.count;
        int h=numBgV.frame.size.height;
        for (int i=0; i<arr.count; i++) {
            x = w*i;
            if (i==1) {
                UILabel *cdxdbmL = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
                cdxdbmL.text = @"0";
                cdxdbmL.textAlignment = NSTextAlignmentCenter;
                [numBgV addSubview:cdxdbmL];
                self.cdxdbmL = cdxdbmL;
            }else{
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(x, y, w, h);
                [btn setTitle:arr[i] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:imgArr[i]] forState:UIControlStateNormal];
                [btn setImage:[UIImage imageNamed:imgArrr[i]] forState:UIControlStateSelected];
                btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
                [numBgV addSubview:btn];
                [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                if (i==0) {
                    self.jianBtn = btn;
                }else if(i==2){
                    self.pluseBtn = btn;
                }
            }
        }
        
    }
    return self;
}
-(void)btnAction:(UIButton *)btn{
    if ([btn.titleLabel.text isEqualToString:@"+"]) {
        if (_pluseBlock) {
            _pluseBlock(_cdxdbmL.text);
        }
    }else{
        
        if (_jianBlock) {
            _jianBlock(_cdxdbmL.text);
        }
    }
}
-(void)setDic:(NSDictionary *)dic{
    _dic = dic;
    self.ddbhL.text = [NSString stringWithFormat:@"%@",dic[@"MaterialName"]];
    self.xdrqL.text = [NSString stringWithFormat:@"%@",dic[@"MaterialZJF"]];
    self.xdbmL.text = [NSString stringWithFormat:@"%@",dic[@"MaterialStoreUnit"]];
    self.zdrL.text = [NSString stringWithFormat:@"%@",dic[@"MaterialGG"]];
    self.cdxdbmL.text = [NSString stringWithFormat:@"%@",dic[@"MaterialNum"]];
    self.xdbmLu.text = [NSString stringWithFormat:@"小计：¥%.2f",[[NSString stringWithFormat:@"%@",dic[@"MaterialNum"]] floatValue] * [[NSString stringWithFormat:@"%@",dic[@"MaterialPrice"]] floatValue]];
    if ([[dic allKeys] containsObject:@"MaterialPrice"]) {
        self.zdrLP.text = [NSString stringWithFormat:@"¥%@",dic[@"MaterialPrice"]];
        if ([dic[@"MaterialPrice"] isKindOfClass:[NSNull class]]) {
            self.zdrLP.hidden = YES;
            self.xdbmLu.hidden = YES;
        }else{
            self.zdrLP.hidden = NO;
            self.xdbmLu.hidden = NO;
        }
    }else{
        self.zdrLP.hidden = YES;
        self.xdbmLu.hidden = YES;
    }
}
@end
